package com.hlxt.nj.sys.dao.impl;

import com.hlxt.nj.base.dao.BaseDao;
import com.hlxt.nj.sys.dao.TUserDAO;
import com.hlxt.nj.sys.model.TUser;
import com.hlxt.nj.sys.model.TUserCriteria;
import com.hlxt.nj.common.PageBean;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Repository;


@Repository
public class TUserDAOImpl extends BaseDao implements TUserDAO {

    public TUserDAOImpl() {
        super();
    }

    public int countByExample(TUserCriteria example) {
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("T_USER.countByExample", example);
        return count;
    }

    public int deleteByExample(TUserCriteria example) {
        int rows = getSqlMapClientTemplate().delete("T_USER.deleteByExample", example);
        return rows;
    }

    public int deleteByPrimaryKey(String usrid) {
        TUser _key = new TUser();
        _key.setUsrid(usrid);
        int rows = getSqlMapClientTemplate().delete("T_USER.deleteByPrimaryKey", _key);
        return rows;
    }

    public void insert(TUser record) {
        getSqlMapClientTemplate().insert("T_USER.insert", record);
    }

    public void insertSelective(TUser record) {
        getSqlMapClientTemplate().insert("T_USER.insertSelective", record);
    }

    @SuppressWarnings("unchecked")
    public List<TUser> selectByExample(TUserCriteria example) {
        List<TUser> list = getSqlMapClientTemplate().queryForList("T_USER.selectByExample", example);
        return list;
    }

    public TUser selectByPrimaryKey(String usrid) {
        TUser _key = new TUser();
        _key.setUsrid(usrid);
        TUser record = (TUser) getSqlMapClientTemplate().queryForObject("T_USER.selectByPrimaryKey", _key);
        return record;
    }

    public int updateByExampleSelective(TUser record, TUserCriteria example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("T_USER.updateByExampleSelective", parms);
        return rows;
    }

    public int updateByExample(TUser record, TUserCriteria example) {
        UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
        int rows = getSqlMapClientTemplate().update("T_USER.updateByExample", parms);
        return rows;
    }

    public int updateByPrimaryKeySelective(TUser record) {
        int rows = getSqlMapClientTemplate().update("T_USER.updateByPrimaryKeySelective", record);
        return rows;
    }

    public int updateByPrimaryKey(TUser record) {
        int rows = getSqlMapClientTemplate().update("T_USER.updateByPrimaryKey", record);
        return rows;
    }

    @SuppressWarnings("unchecked")
    public void selectPageBeanByExample(TUserCriteria example, PageBean pageBean) {
        example.setFirstResult(pageBean.getRsFirstNumber() - 1);
        example.setMaxResults(pageBean.getLength());
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("T_USER.countByExample", example);
        List<TUser> list = getSqlMapClientTemplate().queryForList("T_USER.selectByExample", example);
        pageBean.setTotalRecords(count);
        pageBean.setResults(list);
    }

    @SuppressWarnings("unchecked")
    public void selectPageBean(TUser record, PageBean pageBean) {
        Map<String,Object> condition = new HashMap<String,Object>();
        try {
            condition = PropertyUtils.describe(record);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        condition.put("firstResult", pageBean.getRsFirstNumber() - 1);
        condition.put("endResult", pageBean.getLength() + pageBean.getRsFirstNumber() - 1);
        condition.put("maxResults", pageBean.getLength());
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("T_USER.countByPo", record);
        List<TUser> list = getSqlMapClientTemplate().queryForList("T_USER.selectPageBeanByMap", condition);
        pageBean.setTotalRecords(count);
        pageBean.setResults(list);
    }

    public int count(TUser record) {
        Integer count = (Integer)  getSqlMapClientTemplate().queryForObject("T_USER.countByPo", record);
        return count;
    }

    @SuppressWarnings("unchecked")
    public List<TUser> select(TUser record) {
        Map<String,Object> condition = new HashMap<String,Object>();
        try {
            condition = PropertyUtils.describe(record);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        condition.put("firstResult", null);
        condition.put("endResult", null);
        condition.put("maxResults", null);
        List<TUser> list = getSqlMapClientTemplate().queryForList("T_USER.selectPageBeanByMap", condition);
        return list;
    }

    protected static class UpdateByExampleParms extends TUserCriteria {
        private Object record;

        public UpdateByExampleParms(Object record, TUserCriteria example) {
            super(example);
            this.record = record;
        }

        public Object getRecord() {
            return record;
        }
    }
}